package com.jic.member.impl.power;
import	java.util.ArrayList;

import java.lang.Long;
import java.util.List;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.api.rest.ManagerMerchantApi;
import com.jic.manager.api.rest.ManagerStoreApi;
import com.jic.manager.request.MerchantRequest;
import com.jic.manager.request.MestoreRequest;
import com.jic.manager.request.SysDeptRequest;
import com.jic.manager.response.MestoreResponse;
import com.jic.manager.response.SysDeptResponse;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MepowerProduct;
import com.jic.member.entity.power.MepowerStore;
import com.jic.member.entity.power.Meproduct;
import com.jic.member.mapper.power.MepowerProductMapper;
import com.jic.member.mapper.power.MepowerStoreMapper;
import com.jic.member.mapper.power.MeproductMapper;
import com.jic.member.request.power.MepowerStoreRequest;
import com.jic.member.request.power.PowerStoreRequest;
import com.jic.member.service.power.MepowerStoreService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 权益店铺关联表
 * @author : 
 * @email : 
 * @since : 2020-05-11 02:44:42
 * @version : v1.0.0
 */
@Service
public class MepowerStoreServiceImpl implements MepowerStoreService {

    @Resource
    private MepowerStoreMapper mepowerStoreMapper;
    @Resource
    MeproductMapper meProductMap;
    @Resource
    private SysDeptApi sysDeptService;
    @Resource
    private MepowerProductMapper mepowerProductMapper;
    @Resource
    private ManagerStoreApi mestoreService;
    @Resource
    private ManagerMerchantApi managerMerchantApi;

    @Override
    public long insert(MepowerStore mepowerStore) {
        long flag=0;
        MepowerStore insert = new MepowerStore();
        MepowerStore query = new MepowerStore();
        List<PowerStoreRequest> storeList = mepowerStore.getStoreList();
        if (CollectionUtils.isNotEmpty(storeList)){
            for (int i = 0; i < storeList.size(); i++){
                query.setLegalRuleId(storeList.get(i).getLegalRuleId());
                query.setStoreId(storeList.get(i).getStoreId());
                query.setDeleteFlag(0);
                List<MepowerStore> resultList = mepowerStoreMapper.query(query);
                if(CollectionUtils.isNotEmpty(resultList)){
                    continue;
                }
                insert.setLegalRuleId(storeList.get(i).getLegalRuleId());
                insert.setStoreId(storeList.get(i).getStoreId());
                insert.setStoreName(storeList.get(i).getStoreName());
                insert.setStoreCode(storeList.get(i).getStoreCode());
                insert.setMerchantId(storeList.get(i).getMerchantId());
                insert.setStoreType(storeList.get(i).getStoreType());
                insert.setMerchantId(storeList.get(i).getMerchantId());
                insert.setMerchantName(getMerchanName(storeList.get(i).getMerchantId()));
                insert.setDeleteFlag(0);
                mepowerStoreMapper.insert(insert);
            }
        }
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mepowerStoreMapper.delete(id);
    }

    @Override
    public long update(MepowerStore mepowerStore) {
        return mepowerStoreMapper.update(mepowerStore);
    }

    @Override
    public MepowerStore queryByPrimaryKey(Long id){
        return mepowerStoreMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MepowerStore> query(MepowerStore mepowerStore){
        return mepowerStoreMapper.query(mepowerStore);
    }

    @Override
    public PageResult<MepowerStore> queryPage(MepowerStore mepowerStore, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        mepowerStore.setDeleteFlag(0);
        List<MepowerStore> list = mepowerStoreMapper.queryPage(startIndex, pageQuery.getLimit(), mepowerStore);
        long count = mepowerStoreMapper.queryPageCount(mepowerStore);
        list.forEach(item -> {
            RestResult<String> merchant = sysDeptService.queryDeptNameById(item.getMerchantId());
            item.setChildName(getChildName(item.getMerchantId()));
            item.setMerchantName(merchant.getData());
        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public void deleteInfo(MepowerStore memberRequest) {
        if (null != memberRequest){
            memberRequest.getIds().forEach(item -> {
                mepowerStoreMapper.delete(item);
                // 删除店铺的同时 删除旗下所属商品
                // 查询当前权益店铺信息
                MepowerStore mepowerStore = mepowerStoreMapper.queryByPrimaryKey(item);
                MepowerProduct mepowerProduct = new MepowerProduct();
                if (null != mepowerStore) {
                    //查询mem_power_product
                    mepowerProduct.setLegalRuleId(mepowerStore.getLegalRuleId());
                    List<MepowerProduct> resultProduct = mepowerProductMapper.query(mepowerProduct);
                    if(CollectionUtils.isNotEmpty(resultProduct)){
                        Meproduct meProduct = new Meproduct();
                        meProduct.setPowerProductId(resultProduct.get(0).getId().longValue());
                        meProduct.setStoreId(mepowerStore.getStoreId());
                        List<Meproduct> result = meProductMap.query(meProduct);
                        result.forEach(product -> {
                            meProductMap.delete(product.getId().longValue());
                        });
                    }
                }
            });
        }
    }

    @Override
    public PageResult<MestoreResponse> queryStorePage(MepowerStoreRequest form, Page page) {
        MestoreRequest storeReuqest = BeanUtil.copy(form, MestoreRequest.class);
        storeReuqest.setStart(page.getStart());
        storeReuqest.setLimit(page.getLimit());
        PageResult<MestoreResponse> storeList = mestoreService.queryPage(storeReuqest);
        return storeList;
    }

    @Override
    public void saveAllStoreInfo(MepowerStore memberRequest) {
        Page page = new Page();
        MepowerStoreRequest storeRequest = new MepowerStoreRequest();
        storeRequest.setAdminOperate(memberRequest.getAdminOperate());
        page.setStart(1);
        page.setLimit(1000000);
        PageResult<MestoreResponse> memStoreResponsePageResult = this.queryStorePage(storeRequest, page);
        MepowerStore insert = new MepowerStore();
        MepowerStore query = new MepowerStore();
        List<MestoreResponse> storeList = memStoreResponsePageResult.getRows();
        for (int i = 0; i < storeList.size();i++){
            query.setLegalRuleId(memberRequest.getLegalRuleId());
            query.setStoreId(storeList.get(i).getId());
            query.setDeleteFlag(0);
            List<MepowerStore> resultList = mepowerStoreMapper.query(query);
            if(CollectionUtils.isNotEmpty(resultList)){
                continue;
            }
            insert.setLegalRuleId(memberRequest.getLegalRuleId());
            insert.setStoreId(storeList.get(i).getId());
            insert.setStoreName(storeList.get(i).getStoreName());
            insert.setStoreCode(storeList.get(i).getStoreCode());
            insert.setMerchantId(storeList.get(i).getDeptId());
            insert.setMerchantName(getMerchanName(storeList.get(i).getDeptId()));
            insert.setStoreType(storeList.get(i).getStoreType());
            insert.setDeleteFlag(0);
            mepowerStoreMapper.insert(insert);
        }

    }

    public String getChildName(Long businessId) {
        SysDeptRequest queryMerchant = new SysDeptRequest();
        SysDeptRequest queryChild = new SysDeptRequest();
        queryMerchant.setBusinessId(businessId);
        queryMerchant.setDeptType(2);
        List<SysDeptResponse> resultMerchant = sysDeptService.query(queryMerchant);
        if (CollectionUtils.isNotEmpty(resultMerchant)){
            queryChild.setId(resultMerchant.get(0).getPid());
            List<SysDeptResponse> resultChild = sysDeptService.query(queryChild);
            return resultChild.get(0).getDeptName();
        }
        return null;
    }

    private String getMerchanName(Long merchantId){
        List<Long> merchantList = new ArrayList<>();
        merchantList.add(merchantId);
        RestResult<List<MerchantRequest>> resultList = managerMerchantApi.queryMerchantListByid(merchantList);
        if(null != resultList && null != resultList.getData() && !resultList.getData().isEmpty()){
            return resultList.getData().get(0).getName();
        }
        return null;
    }
}